//
// Copyright (C) 2011 - 2013  Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.2 with QuantLib 1.2.1
//#include "stdafx.h"
#include "FdmSchemeDesc.h"
using namespace Cephei::QL::Methods::Finitedifferences::Solvers;
#undef HANDLE
#undef ABSTRACT
#define STRUCT
Cephei::QL::Methods::Finitedifferences::Solvers::CFdmSchemeDesc::CFdmSchemeDesc (QL::Methods::Finitedifferences::Solvers::FdmSchemeDesc::FdmSchemeTypeEnum type, Double theta, Double mu) 
{
	_pSpinlock = new boost::detail::spinlock ();
    try
    {
#ifdef HANDLE
        _phFdmSchemeDesc = NULL;
#endif
        QuantLib::FdmSchemeDesc::FdmSchemeType _type = (QuantLib::FdmSchemeDesc::FdmSchemeType)type ;
        QuantLib::Real _theta = (QuantLib::Real)ValueHelper::Convert (theta); //d
        QuantLib::Real _mu = (QuantLib::Real)ValueHelper::Convert (mu); //d
        _ppFdmSchemeDesc = new boost::shared_ptr<QuantLib::FdmSchemeDesc> (new QuantLib::FdmSchemeDesc ( _type,  _theta,  _mu ));
        
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Methods::Finitedifferences::Solvers::CFdmSchemeDesc::CFdmSchemeDesc (boost::shared_ptr<QuantLib::FdmSchemeDesc>& childNative, Object^ owner) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phFdmSchemeDesc = NULL;
#endif
	_ppFdmSchemeDesc = &childNative;
    
}
Cephei::QL::Methods::Finitedifferences::Solvers::CFdmSchemeDesc::CFdmSchemeDesc (QuantLib::FdmSchemeDesc& childNative, Object^ owner) 
{
#ifdef HANDLE
	_phFdmSchemeDesc = NULL;
#endif
	_ppFdmSchemeDesc = new boost::shared_ptr<QuantLib::FdmSchemeDesc> (&childNative);
    
    _FdmSchemeDescOwner = owner;
    
}

Cephei::QL::Methods::Finitedifferences::Solvers::CFdmSchemeDesc::CFdmSchemeDesc (CFdmSchemeDesc^ copy) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phFdmSchemeDesc = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppFdmSchemeDesc = new boost::shared_ptr<QuantLib::FdmSchemeDesc> (copy->GetShared());
        
    }
}
Cephei::QL::Methods::Finitedifferences::Solvers::CFdmSchemeDesc::CFdmSchemeDesc (PLATFORM::Type^ t) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phFdmSchemeDesc = NULL;
#endif
	if (!t->IsSubclassOf(CFdmSchemeDesc::typeid))
		throw REFNEW Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Methods::Finitedifferences::Solvers::CFdmSchemeDesc::CFdmSchemeDesc (QuantLib::Handle<QuantLib::FdmSchemeDesc>& childNative, Object^ owner)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phFdmSchemeDesc = &childNative;
	_ppFdmSchemeDesc = &static_cast<boost::shared_ptr<QuantLib::FdmSchemeDesc>>(childNative.currentLink());
    
    _FdmSchemeDescOwner = owner;
}
Cephei::QL::Methods::Finitedifferences::Solvers::CFdmSchemeDesc::CFdmSchemeDesc (QuantLib::Handle<QuantLib::FdmSchemeDesc> childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phFdmSchemeDesc = &childNative;
	_ppFdmSchemeDesc = &static_cast<boost::shared_ptr<QuantLib::FdmSchemeDesc>>(childNative.currentLink());
    
}
#endif
#ifdef STRUCT
Cephei::QL::Methods::Finitedifferences::Solvers::CFdmSchemeDesc::CFdmSchemeDesc (QuantLib::FdmSchemeDesc childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phFdmSchemeDesc = NULL;
#endif
	_ppFdmSchemeDesc = new boost::shared_ptr<QuantLib::FdmSchemeDesc> (new QuantLib::FdmSchemeDesc (childNative));
    
}
#endif

Cephei::QL::Methods::Finitedifferences::Solvers::CFdmSchemeDesc::~CFdmSchemeDesc ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
        _pSpinlock = NULL;
    }
    if (_ppFdmSchemeDesc != NULL)
    {
	    delete _ppFdmSchemeDesc;
        _ppFdmSchemeDesc = NULL;
    }
}
Cephei::QL::Methods::Finitedifferences::Solvers::CFdmSchemeDesc::!CFdmSchemeDesc ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
    }
    if (_ppFdmSchemeDesc != NULL)
    {
	    delete _ppFdmSchemeDesc;
    }
}
QuantLib::FdmSchemeDesc& Cephei::QL::Methods::Finitedifferences::Solvers::CFdmSchemeDesc::GetReference ()
{
    if (_ppFdmSchemeDesc == NULL) throw REFNEW NativeNullException ();
	return **_ppFdmSchemeDesc;
}
boost::shared_ptr<QuantLib::FdmSchemeDesc>& Cephei::QL::Methods::Finitedifferences::Solvers::CFdmSchemeDesc::GetShared ()
{
    if (_ppFdmSchemeDesc == NULL) throw REFNEW NativeNullException ();
	return *_ppFdmSchemeDesc;
}
QuantLib::FdmSchemeDesc* Cephei::QL::Methods::Finitedifferences::Solvers::CFdmSchemeDesc::GetPointer ()
{
    if (_ppFdmSchemeDesc == NULL) throw REFNEW NativeNullException ();
	return &**_ppFdmSchemeDesc;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::FdmSchemeDesc>& Cephei::QL::Methods::Finitedifferences::Solvers::CFdmSchemeDesc::GetHandle ()
{
	if (_phFdmSchemeDesc == NULL)
	{
		_phFdmSchemeDesc = new Handle<QuantLib::FdmSchemeDesc> (*_ppFdmSchemeDesc);
	}
	return *_phFdmSchemeDesc;
}
#endif
bool Cephei::QL::Methods::Finitedifferences::Solvers::CFdmSchemeDesc::HasNative () 
{
	return (_ppFdmSchemeDesc != NULL);
}

Cephei::QL::Methods::Finitedifferences::Solvers::IFdmSchemeDesc^ Cephei::QL::Methods::Finitedifferences::Solvers::CFdmSchemeDesc::CraigSneyd::get ()
{
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
    	QuantLib::FdmSchemeDesc _rv = (QuantLib::FdmSchemeDesc)(*_ppFdmSchemeDesc)->CraigSneyd ( );   
        Cephei::QL::Methods::Finitedifferences::Solvers::CFdmSchemeDesc^ _nrv = REFNEW Cephei::QL::Methods::Finitedifferences::Solvers::CFdmSchemeDesc (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Methods::Finitedifferences::Solvers::IFdmSchemeDesc^ Cephei::QL::Methods::Finitedifferences::Solvers::CFdmSchemeDesc::Douglas::get ()
{
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
    	QuantLib::FdmSchemeDesc _rv = (QuantLib::FdmSchemeDesc)(*_ppFdmSchemeDesc)->Douglas ( );   
        Cephei::QL::Methods::Finitedifferences::Solvers::CFdmSchemeDesc^ _nrv = REFNEW Cephei::QL::Methods::Finitedifferences::Solvers::CFdmSchemeDesc (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Methods::Finitedifferences::Solvers::IFdmSchemeDesc^ Cephei::QL::Methods::Finitedifferences::Solvers::CFdmSchemeDesc::ExplicitEuler::get ()
{
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
    	QuantLib::FdmSchemeDesc _rv = (QuantLib::FdmSchemeDesc)(*_ppFdmSchemeDesc)->ExplicitEuler ( );   
        Cephei::QL::Methods::Finitedifferences::Solvers::CFdmSchemeDesc^ _nrv = REFNEW Cephei::QL::Methods::Finitedifferences::Solvers::CFdmSchemeDesc (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Methods::Finitedifferences::Solvers::IFdmSchemeDesc^ Cephei::QL::Methods::Finitedifferences::Solvers::CFdmSchemeDesc::Hundsdorfer::get ()
{
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
    	QuantLib::FdmSchemeDesc _rv = (QuantLib::FdmSchemeDesc)(*_ppFdmSchemeDesc)->Hundsdorfer ( );   
        Cephei::QL::Methods::Finitedifferences::Solvers::CFdmSchemeDesc^ _nrv = REFNEW Cephei::QL::Methods::Finitedifferences::Solvers::CFdmSchemeDesc (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Methods::Finitedifferences::Solvers::IFdmSchemeDesc^ Cephei::QL::Methods::Finitedifferences::Solvers::CFdmSchemeDesc::ImplicitEuler::get ()
{
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
    	QuantLib::FdmSchemeDesc _rv = (QuantLib::FdmSchemeDesc)(*_ppFdmSchemeDesc)->ImplicitEuler ( );   
        Cephei::QL::Methods::Finitedifferences::Solvers::CFdmSchemeDesc^ _nrv = REFNEW Cephei::QL::Methods::Finitedifferences::Solvers::CFdmSchemeDesc (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Methods::Finitedifferences::Solvers::IFdmSchemeDesc^ Cephei::QL::Methods::Finitedifferences::Solvers::CFdmSchemeDesc::ModifiedCraigSneyd::get ()
{
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
    	QuantLib::FdmSchemeDesc _rv = (QuantLib::FdmSchemeDesc)(*_ppFdmSchemeDesc)->ModifiedCraigSneyd ( );   
        Cephei::QL::Methods::Finitedifferences::Solvers::CFdmSchemeDesc^ _nrv = REFNEW Cephei::QL::Methods::Finitedifferences::Solvers::CFdmSchemeDesc (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Methods::Finitedifferences::Solvers::IFdmSchemeDesc^ Cephei::QL::Methods::Finitedifferences::Solvers::CFdmSchemeDesc::ModifiedHundsdorfer::get ()
{
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
    	QuantLib::FdmSchemeDesc _rv = (QuantLib::FdmSchemeDesc)(*_ppFdmSchemeDesc)->ModifiedHundsdorfer ( );   
        Cephei::QL::Methods::Finitedifferences::Solvers::CFdmSchemeDesc^ _nrv = REFNEW Cephei::QL::Methods::Finitedifferences::Solvers::CFdmSchemeDesc (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

Cephei::QL::Methods::Finitedifferences::Solvers::IFdmSchemeDesc^ Cephei::QL::Methods::Finitedifferences::Solvers::CFdmSchemeDesc_Factory::Create (QL::Methods::Finitedifferences::Solvers::FdmSchemeDesc::FdmSchemeTypeEnum type, Double theta, Double mu)
{
    return REFNEW CFdmSchemeDesc ( type,  theta,  mu);
}
